//
//  LDYTabbar.swift
//  李东阳微博
//
//  Created by 李东阳 on 16/4/16.
//  Copyright © 2016年 李东阳. All rights reserved.
//

import UIKit
protocol LDYTabbarDelegate : NSObjectProtocol {

    // 协议方法
    func composeButtonClick()

}


class LDYTabbar: UITabBar {


    weak var ldyDelegate:LDYTabbarDelegate?
    
  
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
    

       fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    
    
    
    
    
        //MARK: - composeButton 的点击事件
    
    @objc private func composeButtonClick(){
    
     ldyDelegate?.composeButtonClick()
    
    }

    
        //MARK: -  添加子控件
    func setupUI(){
    
    addSubview(composeButton)
        
    }

    
        //MARK: -   设置子控件的frame
    override func layoutSubviews() {
        
        super.layoutSubviews()

            //MARK: -  设置子控件的frame
        let subWidth = UIScreeonwidth / 5
        
            //MARK: -  使用标示
        var index: CGFloat = 0
        
        for subV in self.subviews {
        
                //MARK: -  判断先判断一下子控件是否是我们需要的子控件UITabBarButton
            if subV .isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                    //MARK: -  只需要设置 x width

            subV.frame.origin.x = index * subWidth
                
             subV.frame.size.width = subWidth
                
                    index++
                
            }
            
         
            
            if index == 2 {
            
            index++
            
            }
        
        
        }

      //设置 composeButton
        
        composeButton.center.x = frame.size.width / 2
        
       composeButton.center.y = frame.size.height / 2
        
        
        
    }

    
    
  //mark-- 懒加载composeButton
  

    private lazy var composeButton: UIButton = {
    
            //MARK: -  初始化button
       let comButton = UIButton()
        
            //MARK: -  设置属性
      comButton .setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
      comButton.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
    
      comButton.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
      comButton.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
            //MARK: -  添加点击事件
      comButton.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
    
       comButton.sizeToFit()
        return comButton
        
    }()
   
    
   
    
}
